package com.blueocean_health.care.management.controller;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.em.LocalManagerType;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.domain.base.PostageV;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.ordervo.PostageVo;
import com.blueocean_health.care.management.service.PostageService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping(value = "/web/postage")
public class PostageController {

	@Resource
	private PostageService postageService;

	@RequestMapping("/list")
	public SysResult selectListPostage(@RequestBody(required = false) PostageVo postageVVo) {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hid = courrentUser.getHospitalId();
		postageVVo.setHospitalId(hid);
		PageInfo<PostageV> selectListPostage = postageService.selectListPostage(postageVVo);
		return SysResultByPage.ok(selectListPostage.getList(), selectListPostage.getTotal());
	}
	@RequestMapping("/listExcel")
	public void selectListPostageExcel( PostageVo postageVVo,HttpServletResponse response) throws Exception {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hid = courrentUser.getHospitalId();
		postageVVo.setHospitalId(hid);
		List<PostageV> selectListPostage = postageService.selectListPostageExcel(postageVVo);
		String finalFileName = URLEncoder.encode("资费管理"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
		response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		ExcelUtil.getInstance().exportObj2Excel(toClient, selectListPostage, PostageV.class);
	}
	@RequestMapping("/add")
	public SysResult addPostage(@RequestBody PostageVo postageVV) {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hid = courrentUser.getHospitalId();
		postageVV.setHospitalId(hid);
		int addPostage = postageService.addPostage(postageVV);
		
		if (addPostage > 0) {
			return SysResult.ok();
		} else if(addPostage == -1) {
			return SysResult.error("该资费组合已存在，请勿重复提交");
		} 
		else {
			return SysResult.error("插入失败");
		}
	}

	@RequestMapping("/query")
	public SysResult queryPostage(@RequestBody PostageVo postageVV) {
		PostageV postage = postageService.queryPostage(postageVV.getId());
		return SysResult.ok(postage);
	}

	@RequestMapping("/update")
	public SysResult updatePostage(@RequestBody PostageVo postageVV) {
		int postage = postageService.updatePostage(postageVV);
		if (postage > 0) {
			return SysResult.ok();
		} else {
			return SysResult.error("修改失败");
		}

	}
	@RequestMapping("/delete")
	public SysResult deletePostage(@RequestBody PostageVo postageVV) {
		int postage = postageService.deletePostage(postageVV);
		if (postage > 0) {
			return SysResult.ok();
		} else {
			return SysResult.error("删除失败");
		}
	}
	
}
